<?php
namespace Api\Controller;
use Think\Controller;
class MeetController extends Controller {
	public function _initialize(){
		$this -> company = M('Company');
        $this -> meetfcla = M('Meetforclass');
        $this -> mc = M('Meetcompany');
        $this -> mu = M('Meetuser');
        $this -> usercom = M('Usercom');
        $this -> ucview = M('Ucview');

	}

    public function addusercom(){
        $data = array(
            'uid' => $_POST['uid'],
            'cid' => $_POST['cid']
            );

        $has = $this -> usercom -> where($data) -> select();
        if (empty($has)) {
            $data['ctime'] = time();
            $data['utime'] = time();
            $data['status'] = 0;
            $res = $this -> usercom -> add($data);
            apiResponse("success","关注成功！",$res);
        }else{
            apiResponse("error","您已关注过了！");
        }

    }

    public function getcare(){
        $w['status'] = array('neq',9);
        $w['uid'] = $_POST['uid'];
        $res = $this -> ucview -> where($w) -> select();
        if (!empty($res)) {
            apiResponse("success","返回正常！",$res);
        }else{
            apiResponse("error","没数据了！");
        }
    }

    public function cominfo(){
        $where['id'] = $_POST['cid'];
        $where['status'] = array('neq',9);
        $res = $this -> company -> where( $where ) -> select();
        if ( !empty( $res ) ) {
            apiResponse("success","获取数据成功！",$res[0]);
        }else{
            apiResponse("error","获取数据失败");
        }
    }

    public function addcomp(){
        $data = array(
            'name' => $_POST['name'],
            'tel' => $_POST['tel']
            );
        $has = $this -> company -> where($data) -> limit(1) -> select();
        if (empty($has)) {
            $data['people'] = $_POST['people'];
            $data['position'] = $_POST['position'];
            $data['email'] = $_POST['email'];
            $data['status'] = 0;
            $data['ctime'] = time();
            $data['utime'] = time();
            $cid = $this -> company -> add($data);
        }else{
            $cid = $has[0]['id'];
        }
        //完善已报名校验
        $mc = array(
            'cid' => $cid,
            'mid' => $_POST['mid']
            );
        $has1 = $this -> mc -> where($mc) -> select();
        if (!empty($has1)) {
            apiResponse("success","不能重复报名！");
            exit();
        }
        $mc['ctime'] = time();
        $mc['utime'] = time();
        $mc['status'] = 0;
        $res = $this -> mc -> add( $mc );
        if (!empty($res)) {
            apiResponse("success","申请报名成功，等待管理员审核！",$res);
        }else{
            apiResponse("error","申请失败！");
        }
    }


    public function adduser(){
        //完善已报名校验
        $mu = array(
            'uid' => $_POST['uid'],
            'mid' => $_POST['mid']
            );
        $has = $this -> mu -> where($mu) -> select();
        if (!empty($has)) {
            apiResponse("success","不能重复报名！");
            exit();
        }

        $mu['ctime'] = time();
        $mu['utime'] = time();
        $mu['status'] = 1;
        $res = $this -> mu -> add( $mu );
        if (!empty($res)) {
            apiResponse("success","申请报名成功！",$res);
        }else{
            apiResponse("error","申请失败！");
        }
    }
    
    public function addreport(){
        $data = array(
            'uid' => $_POST['uid'],
            'content' => $_POST['content'],
            'pic' => $_POST['pic']
            );
        $res = M('Report') -> add($data);
        if ($res) {
            apiResponse("success","添加成功！");
        }else{
            apiResponse("error","反馈失败！");
        }
    }

    public function getmeets(){
        $w['status'] = array('neq',9);
        if ($_POST['cid']) {
            $w['cid'] = $_POST['cid'];
            $res = M('mc') -> where($w) -> select();
        }else{
            $w['uid'] = $_POST['uid'];
            $res = M('mu') -> where($w) -> select();
        }
        if (!empty($res)) {
            apiResponse("success","查询成功！",$res);
        }else{
            apiResponse("error","查询失败！");
        }
    }    

    function apiResponse($flag = 'error', $message = '',$data = array()){
        $result = array('flag'=>$flag,'message'=>$message,'data'=>$data);
        print json_encode($result);exit;
    }
}